package org.ee.system.domain;

import jakarta.persistence.*;
import org.ee.framework.jpa.base.AbstractEntity;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import java.util.ArrayList;
import java.util.List;

/**
 * 组织架构
 * @Author jokr
 * @Date 2025/8/27 下午5:32
 */
@Entity
@Table(name="sys_organization")
@DynamicUpdate
@DynamicInsert
@Comment("组织架构表")
public class Organization extends AbstractEntity<Long> {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Comment("主键")
    private Long id;

    // 父部门
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id",foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    @Comment("父级ID")
    private Organization parent;

    @Comment("名称")
    private String name;

    @Comment("描述")
    private String description;

    @Comment("级别")
    private Integer level;

    @Comment("排序")
    private Integer sortNum;

    @Comment("状态")
    private Integer status;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "leader_id",foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    @Comment("负责人")
    private UserInfo leader;

    // 下级部门
    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
    private List<Organization> children = new ArrayList<>();

    @Override
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Organization getParent() {
        return parent;
    }

    public void setParent(Organization parent) {
        this.parent = parent;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getLevel() {
        return level;
    }

    public void setLevel(Integer level) {
        this.level = level;
    }

    public Integer getSortNum() {
        return sortNum;
    }

    public void setSortNum(Integer sortNum) {
        this.sortNum = sortNum;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public UserInfo getLeader() {
        return leader;
    }

    public void setLeader(UserInfo leader) {
        this.leader = leader;
    }

    public List<Organization> getChildren() {
        return children;
    }

    public void setChildren(List<Organization> children) {
        this.children = children;
    }

    // 计算下级节点数量
    public int getChildCount() {
        return children != null ? children.size() : 0;
    }
}
